//
//  LTWelcomeViewController.swift
//  微博08
//
//  Created by 123 on 16/3/4.
//  Copyright © 2016年 Tsr. All rights reserved.
//

import UIKit

class LTWelcomeViewController: UIViewController {
    
    // 属性
    // iconView的底部约束
    var iconViewCons: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
    }
    
    // 界面出来后调用这个方法
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAnimation()
    }
    
    private func prepareUI() {
        // 添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 设置约束
        // 背景
        // VFL: 可视化
        /*
        format:
        H: 水平方向约束 | 父控件的左边  | 父控件的右边
        V: 垂直方向约束 | 父控件的顶部  | 父控件的底部
        */
        
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkgImage]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgImage" : bkgImageView]))
        
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkgImage]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgImage" : bkgImageView]))
        
        // 头像
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: bkgImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        iconViewCons = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bkgImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconViewCons!)
        
        // label 
        
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        
    }
    
    // MARK: 动画方法
    private func moveAnimation() {
        iconViewCons?.constant = -(UIScreen.mainScreen().bounds.size.height - 160)
        
        UIView.animateWithDuration(1.25, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 消息label的渐变动画
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
//                        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
//                        appDelegate.switchRootViewController(true)
                        AppDelegate.outSwitchRootViewController(true)
                        
                })
                
        }
    }
    
    // MARK: 懒加载
    // 背景
    lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    // 头像
    lazy var iconView: UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 设置圆角
        iconView.layer.cornerRadius = 42.5
        iconView.clipsToBounds = true
        
        return iconView
    }()
    // label
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "欢迎归来"
        label.sizeToFit()
        label.alpha = 0
        
       return label
    }()
}
